<?Php
	require_once('../Model/getconfig.php');
	//require_once('includes/checksession.php');
	$randomnumer= rand(5, 100000);
	$username=$_SESSION["adminuser"];	
	$adminid=$_SESSION["adminid"];	
?>
<?php
//If the form is submitted
if(isset($_POST['textUsername'])) {

			$username=trim($_REQUEST['textUsername']);
			$password=trim($_REQUEST['textpassword']);	
			$confirmpassword=trim($_REQUEST['textconfirmpassword']);		

//contact  details reading			
			//$RegStatus=$_REQUEST['cmbRegStatus'];
			$Name=trim($_REQUEST['texName']);
			$Address1=trim($_REQUEST['texaddr1']);
			$pincode=trim($_REQUEST['texpincode']);
			$contState=trim($_REQUEST['cmbcontState']);			
			$city=trim($_REQUEST['city']);
			$Email=trim($_REQUEST['textEmail']);				
			$Phone=trim($_REQUEST['textPhone']);
		
			if(username === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid Username!";
			}
			else
			{
				$pnid=0;
				$arrMenu=Model_executivemaster::checkUserexist($username);
				foreach($arrMenu as $objMenu){ 
					$pnid=$objMenu->getuserid();
					if($pnid>0)
					{							
						$hasError = true;				
						$errorMessage = "You are entered username already exist( Enter new username)!";
					}
				}
							
			}
		
			if(password === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid Password!";
			}

			
			if(confirmpassword === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid Password!";
			}
			if($password !== $confirmpassword ) {
				$hasError = true;
				$errorMessage = "Please enter a valid Password ( New password and confirm password are not matching!";
			}	
			
			if(Name === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid Name!";
			}
	
			
			if(Address1 === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid Address!";
			}

			
			if(pincode === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid Pincode!";
			}
			else if (!eregi("^[0-9]", trim($_POST['texpincode']))) {
				$hasError = true;
				$errorMessage = "Please enter a valid Pincode (Digits only)!";
			}				

			
			if(contState === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid state!";
			}

			
			if(city === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid city!";
			}

			
			if(trim($_POST['textEmail']) === '')  {
				$hasError = true;
			} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['textEmail']))) {
				$hasError = true;
				$errorMessage = "Please enter a valid email address!";
			}																										
		//If there is no error, send the email
		if(!isset($hasError)) {	
		
			$arrMenu=Model_maxtab::get_Maxtabvalue('EXECUTIVE');
			foreach($arrMenu as $objMenu){ 
				$maxtabvalue=$objMenu->getValue(); 
				$maxtabvalue=$maxtabvalue+1;		
			}	
			
			$custsql="insert into executivemaster (pn_exeid,c_exeusername,c_exepassword,c_first_name,c_country,c_state,c_city,c_address,	c_pin,c_email,c_mobil,c_iam,c_status) values ('$maxtabvalue','$username','$password','$Name','$contDistrict','$contState','$city','$Address1','$pincode','$Email','$Mobile','IAM01','Y')";		
			//echo $custsql;		
			$objDbManager=new Dbo_DbManager();
			$intMemmasterId=$objDbManager->dbexecute($custsql);		
			
			$maxtabsql="update maxtab set  nvalue='$maxtabvalue' where ctype='EXECUTIVE' ";		
			//echo $custsql;		
			$objDbManager=new Dbo_DbManager();
			$intMemmasterId=$objDbManager->dbexecute($maxtabsql);														
							
			$success=true;
		}
}			
?>		
<?php if(isset($success) == true) { ?>
	<div class="ok_box">
		<h3>Thanks, <?php echo $Name;?></h3>
		<p>New Executive Registration was successfully completed. </p>
	</div>
<?php } ?>

<?php if(isset($hasError) ) { ?>
	<div class="error_box">
		There was an error submitting the form.
		<br />
		<?php echo $errorMessage;?>
	</div>
<?php } ?>